<?php
class Involved_HomeworkPeriodModel extends  BaseModel
{
    protected $table = 'homework_period';
    protected $primaryKey = 'rec_id';


    /**
     * 获取作业中一个课时的详情
     * @param $where
     * @return mixed
     */
    public function getHomeworkPeriodList($where)
    {
        $oQuery = DB::table($this->table);
        if (count($where->period_ids) == 0)
            return array();

        $oQuery->select($this->table.'.homework_id',$this->table.'.period_id',$this->table.'.item_num',
            'period.section','class.section_amount',
            'publish_time','publish_type',$this->table.'.create_time','period.course','period.class_code','period.date')
            ->join('period', 'period.period_id', '=', $this->table.'.period_id')
            ->leftJoin('piece', function($join){
                $join->on('period.teacher_code','=','piece.teacher_code');
                $join->on('period.class_code','=','piece.class_code');
                $join->on('period.order','=','piece.order');

            })
            ->join('class', 'period.class_code', '=', 'class.class_code');

        $oQuery->whereIn('period.period_id', $where->period_ids);
        //课时上课搜索
        if ($where->start_date != '' && $where->end_date != '') {
            $oQuery->whereBetween('piece.date', array($where->start_date, $where->end_date));
        } elseif ($where->start_date != '') {
            $oQuery->where('piece.date', '>=', $where->start_date);
        } elseif ($where->end_date != '') {
            $oQuery->where('piece.date', '<=', $where->end_date);
        }

        if ($where->period_id != '') {
            $oQuery->where('period.period_id', '=', $where->period_id);
        }

        if($where->class_code != '') {
            $oQuery->where('period.class_code', $where->class_code);
        }

        if ($where->student_code != '') {
            $oQuery->join('class_student', 'class_student.class_code', '=', 'period.class_code')
                ->where('class_student.student_code', $where->student_code);
        }

        $oQuery->where('period.teacher_code', $where->teacher_code);
        return $oQuery->get();

    }


    /**
     * 获取一个课时的详情
     * @param $period_id
     * @return mixed
     */
    public function getHomeworkPeriodInfo($period_id)
    {
        $oQuery = DB::table($this->table);
        $oQuery->select($this->table.'.homework_id',$this->table.'.period_id','publish_time','publish_type',$this->table.'.create_time',
            'period.course','period.section','period.class_code')
            ->leftJoin('period', 'period.period_id', '=', $this->table.'.period_id')
            ->where($this->table.'.period_id', $period_id);
        return $oQuery->first();
    }
}
